package com.jerry.walking.module.activityPath.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.walking.R;
import com.jerry.walking.base.BaseApplication;
import com.jerry.walking.base.BaseFragment;
import com.jerry.walking.base.BaseViewHolder;
import com.jerry.walking.base.MyBaseAdapter;
import com.jerry.walking.constant.Urls;
import com.jerry.walking.module.activityPath.activity.SpecialFirst;
import com.jerry.walking.module.activityPath.bean.PathSpecial1;
import com.jerry.walking.utils.OkHttpUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Yang on 2016/5/13.
 *
 *          路线  --->  专题
 *
 */
public class SpecialFragment extends BaseFragment {

    @BindView(R.id.lv_path_special_1)
    PullToRefreshListView listView;

    MyBaseAdapter<PathSpecial1.TypesBean> adapter;

    private List<PathSpecial1.TypesBean> items;

    private ImageLoader imageLoader;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_path_special;
    }

    @Override
    protected void initViews() {
        super.initViews();

        listView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        imageLoader = BaseApplication.getApplication().getImageLoader();

        items = new ArrayList<>();

        //设置并连接适配器
        adapter = new MyBaseAdapter<PathSpecial1.TypesBean>(getActivity(), items) {
            @Override
            public int getLayoutId() {
                return R.layout.adapter_path_special;
            }

            @Override
            public BaseViewHolder getViewHolder() {
                return new ViewHolder();
            }

            @Override
            public void bindViews(View view, BaseViewHolder viewHolder) {
                ViewHolder holder = (ViewHolder) viewHolder;

                DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .cacheOnDisk(true)
                        .cacheInMemory(true)
                        .imageScaleType(ImageScaleType.EXACTLY)
                        .build();
                imageLoader.displayImage(items.get(holder.position).getUrl(), holder.imageView, options);

            }
        };
        listView.setAdapter(adapter);

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                items.clear();
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });

        //设置listview监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SpecialFragment.this.getActivity(), SpecialFirst.class);
                intent.putExtra("id", items.get(position).getId());
                intent.putExtra("name", items.get(position).getName());
                SpecialFragment.this.getActivity().startActivity(intent);
            }
        });
    }

    @Override
    protected void loadData() {
        super.loadData();

        OkHttpUtils.get(Urls.PATH_SPECIAL, new OkHttpUtils.OnGetDataListener() {
            @Override
            public void onResponse(String url, String data) {
                Gson gson = new Gson();

                PathSpecial1 pathSpecial1 = gson.fromJson(data, PathSpecial1.class);

                List<PathSpecial1.TypesBean> types = pathSpecial1.getTypes();

                items.addAll(types);

                adapter.notifyDataSetChanged();
                listView.onRefreshComplete();
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });

    }

    /**
     *    viewHolder ,adapter 专用
     */
    class ViewHolder extends BaseViewHolder {
        @BindView(R.id.iv_path_special_adapter)
        ImageView imageView;
    }
}
